package com.trace.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trace.app.entity.ResponseResult;
import com.trace.app.entity.User;
import com.trace.app.entity.consts.AppHttpCodeEnum;
import com.trace.app.entity.vo.LabelValueVo;
import com.trace.app.entity.vo.PageVo;
import com.trace.app.mapper.UserMapper;
import com.trace.app.service.UserService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 用户表(User)表服务实现类
 *
 * @author lionkliu
 * @since 2024-03-15 17:27:01
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        Page<User> page = new Page<>(pageNum, pageSize);
        Page<User> userPage = page(page, queryWrapper);
        PageVo<User> pageVo = new PageVo<>();
        pageVo.setResult(userPage.getRecords());
        pageVo.setTotal(userPage.getTotal());
        pageVo.setPageTotal(pageVo.getPageTotal(pageSize));
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult search(Integer pageNum, Integer pageSize, Integer userType, String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        if (!Objects.isNull(username)) {
            queryWrapper.like(User::getUsername, username);
        }
        if (!Objects.isNull(userType)) {
            if (userType < 8) {
                queryWrapper.eq(User::getUserType, userType);
            } else {
                queryWrapper.gt(User::getUserType, 8);
            }
        }
        Page<User> userPage = new Page<>(pageNum, pageSize);
        page(userPage, queryWrapper);
        PageVo<User> userPageVo = new PageVo<>();
        userPageVo.setTotal(userPage.getTotal());
        userPageVo.setPageTotal(userPage.getPages());
        userPageVo.setResult(userPage.getRecords());

        return ResponseResult.okResult(userPageVo);
    }

    @Override
    public ResponseResult getLabelValue() {
        List<LabelValueVo> collect = list().stream().map((user -> {
            LabelValueVo labelValueVo = new LabelValueVo();
            labelValueVo.setValue(String.valueOf(user.getId()));
            labelValueVo.setLabel(user.getUsername());
            return labelValueVo;
        })).collect(Collectors.toList());
        return ResponseResult.okResult(collect);
    }


    @Override
    public ResponseResult selectOne(Integer id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult insertOne(User user) {
        String encoded = passwordEncoder.encode(user.getPassword());
        user.setPassword(encoded);
        return save(user) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(User user) {
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(User::getId, user.getId());
        if (user.getNickName() != null) {
            updateWrapper.set(User::getNickName, user.getNickName());
        }
        if (user.getPhoneNumber() != null) {
            updateWrapper.set(User::getPhoneNumber, user.getPhoneNumber());
        }
        if (user.getEmail() != null) {
            updateWrapper.set(User::getEmail, user.getEmail());
        }
        if (user.getAvatar() != null) {
            updateWrapper.set(User::getAvatar, user.getAvatar());
        }
        if (user.getSex() != null) {
            updateWrapper.set(User::getSex, user.getSex());
        }
        if (user.getStatus() != null) {
            updateWrapper.set(User::getStatus, user.getStatus());
        }
        if (user.getUserType() != null) {
            updateWrapper.set(User::getUserType, user.getUserType());
        }

        return update(null, updateWrapper) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }

    @Override
    public ResponseResult login(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername())
                .eq(User::getPassword, user.getPassword());
        User one = getOne(queryWrapper);
        if (ObjectUtil.isNull(one)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_ERROR);
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult register(User user) {
        insertOne(user);
        return ResponseResult.okResult();
    }

}

